/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Application.java
 *
 * Created on 2/12/2009, 06:50:53 PM
 */

package sahd.gui;

import beans.UserBean;
import java.io.IOException;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Observable;
import java.util.Observer;
import java.util.logging.Level;
import java.util.logging.Logger;
import sahd.broker.client.Client;
import sahd.broker.client.ClientProtocol;
import sahd.broker.proxys.ProxyClient;
import sahd.broker.proxys.ProxyClientNotInstantiatedException;



/**
 *
 * @author Hudy
 */
public class Application extends javax.swing.JFrame implements Observer{

    private String username;
    private int  tryings=3;
    /** Creates new form Application */
    public Application() {
        try {
            initComponents();
            initSecurity();
            Client client = new Client(this, 8888, 7777, 9999, " 192.168.1.67");
            ProxyClient proxyClient = ProxyClient.getInstance();
            proxyClient.shipMessage("Register client", ClientProtocol.REGISTER_CLIENT);
        } catch (IOException ex) {
            Logger.getLogger(Application.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ProxyClientNotInstantiatedException ex) {
            Logger.getLogger(Application.class.getName()).log(Level.SEVERE, null, ex);
        } 

    }
   /**
    *  Inicializa todas las etiquetas del archivo ES_PROPERTIES
    */
    private void initSecurity(){
        mainMenuBar.setVisible(false);
        this.appDesktop.add(loginFrame);
        this.loginFrame.setSize(500, 300);
        loginFrame.setVisible(true);
    }

    private void loguear(String username){
            this.username = username;
            mainMenuBar.setVisible(true);
            loginFrame.setVisible(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        loginFrame = new javax.swing.JInternalFrame();
        loginPanel = new javax.swing.JPanel();
        userLabel = new javax.swing.JLabel();
        userTextField = new javax.swing.JTextField();
        passLabel = new javax.swing.JLabel();
        passField = new javax.swing.JPasswordField();
        loginBtn = new javax.swing.JButton();
        appDesktop = new javax.swing.JDesktopPane();
        mainMenuBar = new javax.swing.JMenuBar();
        sucursalMenu = new javax.swing.JMenu();
        altaSucursalMenuItem = new javax.swing.JMenuItem();
        bajaSucursalMenuItem = new javax.swing.JMenuItem();
        cambioSucursalMenuItem = new javax.swing.JMenuItem();
        consultaSucursalMenuItem = new javax.swing.JMenuItem();
        productoMenu = new javax.swing.JMenu();
        altaProductoMenuItem = new javax.swing.JMenuItem();
        bajaProductoMenuItem = new javax.swing.JMenuItem();
        jMenu1 = new javax.swing.JMenu();
        Sales = new javax.swing.JMenuItem();

        loginFrame.setVisible(true);

        loginPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        userLabel.setText("jLabel1");

        passLabel.setText("jLabel2");

        loginBtn.setText("jButton1");
        loginBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout loginPanelLayout = new javax.swing.GroupLayout(loginPanel);
        loginPanel.setLayout(loginPanelLayout);
        loginPanelLayout.setHorizontalGroup(
            loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(loginPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(userLabel, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(passLabel, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(userTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 192, Short.MAX_VALUE)
                    .addComponent(loginBtn)
                    .addComponent(passField, javax.swing.GroupLayout.DEFAULT_SIZE, 192, Short.MAX_VALUE))
                .addContainerGap(40, Short.MAX_VALUE))
        );
        loginPanelLayout.setVerticalGroup(
            loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(loginPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(userLabel)
                    .addComponent(userTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(passLabel)
                    .addComponent(passField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(loginBtn)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout loginFrameLayout = new javax.swing.GroupLayout(loginFrame.getContentPane());
        loginFrame.getContentPane().setLayout(loginFrameLayout);
        loginFrameLayout.setHorizontalGroup(
            loginFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(loginFrameLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(loginPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        loginFrameLayout.setVerticalGroup(
            loginFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(loginFrameLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(loginPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        appDesktop.setBackground(new java.awt.Color(153, 153, 153));
        appDesktop.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        sucursalMenu.setText("sucursal");

        altaSucursalMenuItem.setText("alta");
        altaSucursalMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                altaSucursalMenuItemActionPerformed(evt);
            }
        });
        sucursalMenu.add(altaSucursalMenuItem);

        bajaSucursalMenuItem.setText("baja");
        sucursalMenu.add(bajaSucursalMenuItem);

        cambioSucursalMenuItem.setText("cambio");
        sucursalMenu.add(cambioSucursalMenuItem);

        consultaSucursalMenuItem.setText("consulta");
        consultaSucursalMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                consultaSucursalMenuItemActionPerformed(evt);
            }
        });
        sucursalMenu.add(consultaSucursalMenuItem);

        mainMenuBar.add(sucursalMenu);

        productoMenu.setText("Product");

        altaProductoMenuItem.setText("alta");
        altaProductoMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                altaProductoMenuItemActionPerformed(evt);
            }
        });
        productoMenu.add(altaProductoMenuItem);

        bajaProductoMenuItem.setText("baja");
        bajaProductoMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bajaProductoMenuItemActionPerformed(evt);
            }
        });
        productoMenu.add(bajaProductoMenuItem);

        mainMenuBar.add(productoMenu);

        jMenu1.setText("Module Sale");

        Sales.setText("Sales");
        jMenu1.add(Sales);

        mainMenuBar.add(jMenu1);

        setJMenuBar(mainMenuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(appDesktop, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(appDesktop, javax.swing.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void altaSucursalMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_altaSucursalMenuItemActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_altaSucursalMenuItemActionPerformed

    private void consultaSucursalMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_consultaSucursalMenuItemActionPerformed
       
}//GEN-LAST:event_consultaSucursalMenuItemActionPerformed

    private void loginBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loginBtnActionPerformed
        this.loguear(username);
        
           

    }//GEN-LAST:event_loginBtnActionPerformed

    private void altaProductoMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_altaProductoMenuItemActionPerformed
        StoreProductInserter altaUI = new StoreProductInserter(this.username);
        this.appDesktop.add(altaUI);
        altaUI.setSize(420, 400);
        altaUI.setVisible(true);
    }//GEN-LAST:event_altaProductoMenuItemActionPerformed

    private void bajaProductoMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bajaProductoMenuItemActionPerformed
        StoreProductDeleter delete = new StoreProductDeleter(this.username);
        this.appDesktop.add(delete);
        delete.setSize(420, 400);
        delete.setVisible(true);
    }//GEN-LAST:event_bajaProductoMenuItemActionPerformed

    



    private void login(String username, String password) {
        
            this.username = username;
            mainMenuBar.setVisible(true);
            UserBean user = new UserBean();
            user.setUserFirstName(username);
            user.setUserAccountPassword(password);
        
    }
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {

                public void run() {
                    Application app = new Application();
                    app.setSize(600, 600);
                    app.setVisible(true);
                }
            });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem Sales;
    private javax.swing.JMenuItem altaProductoMenuItem;
    private javax.swing.JMenuItem altaSucursalMenuItem;
    private javax.swing.JDesktopPane appDesktop;
    private javax.swing.JMenuItem bajaProductoMenuItem;
    private javax.swing.JMenuItem bajaSucursalMenuItem;
    private javax.swing.JMenuItem cambioSucursalMenuItem;
    private javax.swing.JMenuItem consultaSucursalMenuItem;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JButton loginBtn;
    private javax.swing.JInternalFrame loginFrame;
    private javax.swing.JPanel loginPanel;
    private javax.swing.JMenuBar mainMenuBar;
    private javax.swing.JPasswordField passField;
    private javax.swing.JLabel passLabel;
    private javax.swing.JMenu productoMenu;
    private javax.swing.JMenu sucursalMenu;
    private javax.swing.JLabel userLabel;
    private javax.swing.JTextField userTextField;
    // End of variables declaration//GEN-END:variables

    public void update(Observable o, Object arg) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}
